package com.tyj.tuyijiecommon.util;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * <p>
 * 结果统一返回工具类
 * </p>
 *
 * @author lhf
 * @since 2020/6/8
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    private int code;
    private T t;
    private String mes;

    /**
     * 请求成功
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS.code, null, ResultCode.SUCCESS.mes);
    }

    /**
     * 请求成功
     *
     * @param <T>
     * @param t   请求成功的参数
     * @return
     */
    public static <T> Result<T> success(T t) {
        return new Result<>(ResultCode.SUCCESS.code, t, ResultCode.SUCCESS.mes);
    }

    /**
     * 请求失败
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> failure() {
        return new Result<>(ResultCode.FAILURE.code, null, ResultCode.FAILURE.mes);
    }

    /**
     * 请求失败
     *
     * @param <T>
     * @param mes 请求失败的描述
     * @return
     */
    public static <T> Result<T> failure(String mes) {
        return new Result<>(ResultCode.FAILURE.code, null, mes);
    }


    public static enum ResultCode {
        //        请求成功
        SUCCESS(2000, "请求成功"),
        //        请求失败
        FAILURE(5000, "请求失败"),
        //        未登录
        NOT_LOGIN(4000, "未登录"),
        //        登录失败
        LOGIN_FAILURE(4001, "登录失败"),
        //        token 过期
        TOKEN_EX(4002, "TOKEN过期"),

        //        验证码错误
        CODE_EX(4003, "验证码错误");

        private int code;
        private String mes;

        ResultCode(int code, String mes) {
            this.code = code;
            this.mes = mes;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMes() {
            return mes;
        }

        public void setMes(String mes) {
            this.mes = mes;
        }
    }
}
